package com.xuecheng.content.service.impl;

import cn.hutool.core.io.IoUtil;
import com.xuecheng.content.config.MinioTemplate;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import java.io.ByteArrayInputStream;
import java.util.Map;

/**
 * 异步课程发布
 */
@Service
public class AsyncCoursePubService {
    @Autowired
    private Configuration configuration;

    @Autowired
    private MinioTemplate minioTemplate;

    // 异步调用
    @Async
    public void publish(Map map , Long courseBaseId) throws Exception {
        //FreeMaker生成HTML
        //加载模板
        Template template = configuration.getTemplate("learing_article.ftl");
        //输出HTML文件
        String content = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);
        //HTML文本转为io流
        ByteArrayInputStream inputStream = IoUtil.toStream(content.getBytes());
        //保存至minio
        minioTemplate.uploadHtml(inputStream,courseBaseId+".html");
    }
}
